﻿namespace BEGDC.CC.Web.EZVisit.Controls
{
    #region Using Directives

    using System;
    using System.Linq;
    using System.Web.UI.WebControls;
    using ASBCC.Database.Entity;
    using ASBCC.Workflow.K2Manager;
    using BL.Common;
    using BL.UserManagement;
    using Utility.Castle;
    using Utility.Extensions;

    #endregion

    public partial class EZVRequestApplicant : EZVRequestBaseControl
    {
        private bool IsDomestic
        {
            get 
            {
                return this.EZVisitApplication.AppID == null ? DicType.Domestic.VariantEquals(this.Request.QueryString["Site"]) : this.EZVisitApplication.IsDomestic.GetValueOrDefault(true);
            }
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            if (this.Visible && !this.IsPostBack)
            {
                this.PreRenderPartialView();
            }

            // Have to do this to hide the UserSearch control.
            this.edt_AssistantName.Visible = this.edt_ContactName.Visible = this.Editable;
        }

        protected override void SetViewData()
        {
            if (this.EZVisitApplication == null)
            {
                return;
            }

            this.lblApplicantName.Text = string.Format("{0} ({1})<br/> {2}", this.EZVisitApplication.RequesterNameEN, this.EZVisitApplication.RequesterNo, this.EZVisitApplication.RequesterNameCN);

            this.vw_Telephone.Text = this.edt_Telephone.Text = this.EZVisitApplication.RequesterOfficeTel;
            this.vw_Mobile.Text = this.edt_Mobile.Text = this.EZVisitApplication.RequesterMobile;
            this.vw_Title.Text = this.edt_Title.Text = this.EZVisitApplication.RequesterTitle;

            if (this.Editable)
            {
                this.edt_ContactName.TelephoneControlName = this.edt_ContactPhone.ClientID;
                this.edt_ContactName.EmployeeFlowID = this.EZVisitApplication.SomeoneElseID.GetValueOrDefault(0);
                this.edt_ContactName.EmployeeID = this.EZVisitApplication.SomeoneElseNo;
                this.edt_ContactName.EmployeeNameCN = this.EZVisitApplication.SomeoneElseNameCN;
                this.edt_ContactName.Box.Text = this.EZVisitApplication.SomeoneElseNameEN;
                this.edt_ContactPhone.Text = this.EZVisitApplication.SomeoneElseTel;

                this.edt_AssistantName.EmployeeFlowID = this.EZVisitApplication.AssistantID.GetValueOrDefault(0);
                this.edt_AssistantName.EmployeeID = this.EZVisitApplication.AssistantNo;
                this.edt_AssistantName.EmployeeNameCN = this.EZVisitApplication.AssistantIDCN;
                this.edt_AssistantName.Box.Text = this.EZVisitApplication.AssistantIDEN;

                var userService = (IUser)AppContainer.Instance[ServiceConst.UserService];
                this.edt_CheckPerson.DataSource = userService.GetCODPPCheckPersons(this.IsDomestic);
                this.edt_CheckPerson.DataTextField = "NameEN";
                this.edt_CheckPerson.DataValueField = "CheckPersonValue";
                this.edt_CheckPerson.DataBind();

                if (this.EZVisitApplication.CODPPCheckPersonNo.IsNotNullOrWhiteSpace())
                {
                    this.SelectCODPPCheckPerson(this.EZVisitApplication.CODPPCheckPersonNo);
                }
                else
                {
                    var catBranch = (this.Page.FindControl("ddlCATBranch") as DropDownList);

                    if (catBranch != null)
                    {
                        this.ChangeCODPPCheckPerson(Convert.ToInt32(catBranch.SelectedValue.Split(';')[2]));
                    }
                }
            }
            else
            {
                if (this.EZVisitApplication.AssistantID != null)
                {
                    this.vw_AssistantName.Text = string.Format("{0} ({1})<br/> {2}", this.EZVisitApplication.AssistantIDEN, this.EZVisitApplication.AssistantNo, this.EZVisitApplication.AssistantIDCN);
                }
                
                this.vw_ContactName.Text = string.Format("{0} ({1})<br/> {2}", this.EZVisitApplication.SomeoneElseNameEN, this.EZVisitApplication.SomeoneElseNo, this.EZVisitApplication.SomeoneElseNameCN);
                this.vw_ContactPhone.Text = this.EZVisitApplication.SomeoneElseTel;
                this.vw_CheckPerson.Text = string.Format("{0} ({1})<br/> {2}", this.EZVisitApplication.CODPPCheckPersonNameEN, this.EZVisitApplication.CODPPCheckPersonNo, this.EZVisitApplication.CODPPCheckPersonNameCN); ;
            }
        }

        public override void Save(EZVisitApplicationTableEntity app)
        {
            if (app == null)
            {
                return;
            }

            app.RequesterOfficeTel = this.edt_Telephone.Text;
            app.RequesterTitle = this.edt_Title.Text;
            app.RequesterMobile = this.edt_Mobile.Text;

            app.AssistantIDEN = this.edt_AssistantName.EmployeeName;
            app.AssistantIDCN = this.edt_AssistantName.EmployeeNameCN;
            app.AssistantID = this.edt_AssistantName.EmployeeFlowID;
            app.AssistantNo = this.edt_AssistantName.EmployeeID;

            app.SomeoneElseNameEN = this.edt_ContactName.EmployeeName;
            app.SomeoneElseNameCN = this.edt_ContactName.EmployeeNameCN;
            app.SomeoneElseID = this.edt_ContactName.EmployeeFlowID;
            app.SomeoneElseNo = this.edt_ContactName.EmployeeID;
            app.SomeoneElseTel = this.edt_ContactPhone.Text;

            var checkPersonInfo = this.edt_CheckPerson.SelectedValue.Split(';');

            app.CODPPCheckPersonID = Convert.ToInt32(checkPersonInfo[1]);
            app.CODPPCheckPersonNo = checkPersonInfo[0];
            app.CODPPCheckPersonNameCN = checkPersonInfo[2];
            app.CODPPCheckPersonNameEN = this.edt_CheckPerson.SelectedItem.Text;

            base.Save(app);
        }

        public void ChangeCODPPCheckPerson(int departmentId)
        {
            var userService = (IUser)AppContainer.Instance[ServiceConst.UserService];

            this.SelectCODPPCheckPerson(userService.GetCODPPCheckPersonNo(departmentId, this.IsDomestic));
        }

        private void SelectCODPPCheckPerson(string checkPersonNo)
        {
            if (checkPersonNo.IsNotNullOrWhiteSpace())
            {
                this.edt_CheckPerson.ClearSelection();

                foreach (ListItem li in this.edt_CheckPerson.Items.Cast<ListItem>().Where(li => ValueIncludes(li.Value, checkPersonNo)))
                {
                    li.Selected = true;
                    break;
                }
            }
        }

        private static bool ValueIncludes(string src, string dest)
        {
            src = ";" + src + ";";
            dest = ";" + dest + ";";
            return src.IndexOf(dest, StringComparison.InvariantCultureIgnoreCase) != -1;
        }
    }
}